package com.js.rtu.util;

import com.js.rtu.RTUApplication;
import com.js.rtu.config.CmdConfig;

/**
 * 蓝牙收发工具
 * Created by wz on 2020/6/16.
 */
public class BluetoothUtils {
    private  static boolean isRuning=false;
    private final static String mHexStr = "0123456789ABCDEF";

    /**
     * 发送指令
     *
     * @param message      指令
     * @param callback     回调
     */
    public static void send(String message, onCallback callback) {
        send(message , CmdConfig.TIME_OUT,callback);
    }
    /**
     * 发送指令
     *
     * @param message      指令
     * @param milliseconds 超时时间
     * @param callback     回调
     */
    public static void send(String message, int milliseconds, onCallback callback) {
//        if (RTUApplication.bluetoothSocket == null) {
//            callback.onFail("请先连接设备");
//            ToastUtil.info("请先连接设备");
//            return;
//        }
//        if (isRuning){
//            ToastUtil.info("正在处理，请不要多次点击");
//            return;
//        }
//        isRuning=true;
//        RxTimerUtil timerUtil = new RxTimerUtil();
//        timerUtil.timer(1 * 1000, new RxTimerUtil.IRxNext() {
//            @Override
//            public void doNext(long number) {
//                if (isRuning)
////                callback.onFail("超时");
//                isRuning=false;
//
//            }
//        });
        RTUApplication.getInstance().write(message,new BluetoothUtils.onCallback() {
            @Override
            public void onReceive(String msg) {
                isRuning=false;
                callback.onReceive(msg);
            }

            @Override
            public void onFail(String err) {
                isRuning=false;
                callback.onFail(err);
            }
        });




    }



    public interface onCallback {
        void onReceive(String msg);

        void onFail(String err);
    }

}
